
var isjump = 0;//判断是否跳转
var Coupon_id; //(参数名字不要取一样！)
var username;
var secretkey;

layui.config({
	version:'1510164124181'
}).use(['form', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
/*	if(window.sessionStorage.getItem("Coupon_id")){
	//	console.log("Coupon_id=="+window.sessionStorage.getItem("Coupon_id"));
 		var data = JSON.parse(window.sessionStorage.getItem("Coupon_id"));
	//	console.log("data===="+data);
		$("#coupon_type").val(data.coupon_type);	//(类似于session)
		$("#coupon_isuse").val(data.coupon_isuse);
		pageNo = data.pageNo;
 	}*/
	operation_recard(pageRecords,pageNo);
});

//搜索查询用户
function search(){
	isjump=0;
	operation_recard(pageRecords,pageNo);
}

function operation_recard(pageRecords,pageNo){
	var coupon_type = document.getElementById("coupon_type").value;
	var coupon_isuse = document.getElementById("coupon_isuse").value;
	var user_phone = document.getElementById("user_phone").value;

//	Coupon_id='{"coupon_type":"'+coupon_type+'",';
//	Coupon_id+='"coupon_isuse":"'+coupon_isuse+'",';
//	Coupon_id+='"pageNo":"'+pageNo+'"}';
//	window.sessionStorage.setItem("Coupon_id",Coupon_id);
  //console.log("Coupon_id==="+Coupon_id);
  //console.log("参数===="+username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "','" + coupon_type + "','" + coupon_isuse + "','" + user_phone);

	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "g_xmy.coupon_list",
			para:"['" + username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "','" + coupon_type + "','" + coupon_isuse + "','" + user_phone +"']"
		},
		dataType: "json",
		success: function(data){
		//	console.log(JSON.stringify(data));
			var tab=document.getElementById("tableid1");
			var ro=tab.rows.length;
			for(var b=1;;){
				if(tab.rows.length==1){
					break;
				}
				tab.deleteRow(b);
			}
			if (data[0].TAG == "0" ){
				layer.alert(data[0].MSG);
				top.location.href="../../login.html"
			}else{

				for(var a=0;a<data.length;a++){
					if(a==data.length-1){
						totalRecords = data[a].ID;
						totalPage = data[a].USER_PHONE;
						if(isjump ==0){
							page();
							isjump = 1;
						}
						return;
					}
					var tables=document.getElementById("tableid1");
					var ro=tables.insertRow(a+1);
					cell1=ro.insertCell(0);
					cell1.innerHTML=data[a].ID;
					cell1.align="center";
					cell2=ro.insertCell(1);
					cell2.innerHTML=data[a].USER_PHONE;
					cell2.align="center";
					cell3=ro.insertCell(2);
					cell3.innerHTML=data[a].COUPON_TYPE;
					cell3.align="center";
					cell4=ro.insertCell(3);
					cell4.innerHTML=data[a].COUPON_GET_TIME;
					cell4.align="center";
					cell5=ro.insertCell(4);
					cell5.innerHTML=data[a].COUPON_OVER_TIEM;
					cell5.align="center";
					cell6=ro.insertCell(5);
					cell6.innerHTML=data[a].COUPON_VALUE;
					cell6.align="center";
					cell7=ro.insertCell(6);
					cell7.innerHTML=data[a].IS_USE;
					cell7.align="center";
					cell8=ro.insertCell(7);
					cell8.innerHTML='<a href="#" onclick="delete_classification('+data[a].ID+')">删除</a>';
					cell8.align="center";

				}
			}
		}
	});
}
//分页
function page(){
	laypage.render({
		 elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
	//	,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
    //    ,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,jump: function(obj, first){
			//首次不执行
			if(!first){
		//	  console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
		//	  console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
			  operation_recard(obj.limit,obj.curr);
			}
		  }
	});
}
function delete_exeropt(ids){
	top.layer.confirm('清除所有优惠券数据？清除后不可恢复',{icon: 3,title:'提示信息'},function(index){
	var index = top.layer.msg('删除中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			url: "../../../../service",
			cache: false,
			async: false,
			data: {
				proc: "xmy_zyr.delete_exeropt",
				para: "['" + loginname + "','" + loginkey + "','"+ids+"']"
			},
			dataType: "json",
			success: function(data) {
				if(data[0].TAG=='1'){
					layer.closeAll();
					top.layer.msg(""+data[0].MSG+"");
				}else{
					layer.closeAll();
					top.layer.msg(""+data[0].MSG+"");
				}
			}
		});
	});
}
/*
//点击查看跳转页面
function up_classification(id){
	$.session.set("staff_id",id);
	window.location.href="./up_factory.html";
}
*/
//删除
function delete_classification(id){
	var username = $.session.get("username");
	var secretkey = $.session.get("user_secretkey");
	layer.confirm('确定删除该优惠劵记录吗？', {
		  title:'系统提示',
		  btn: ['确定','取消']
		}, function(){
			 $.ajax({
				url: "../../../../service",
				data : {
					proc : "g_xmy.delete_coupon",
					para : "['" + username + "','" + secretkey + "','" + id + "']"
				},
				dataType : "json",
				success : function(data) {
					if (data[0].TAG == "1") {
						layer.alert("成功删除该优惠劵记录记录!",function(index){
							 window.location.href="./card_list.html";
							 layer.close(index);
						});
					}else{
						layer.alert("删除失败!");
					//	window.location.href="./error.html";
					}
				}
			});
		});

}
